package secondBrush.list;

public class E04Leetcode19_2 {
    public static ListNode removeNthFromEnd(ListNode head, int n) {
        //先获取head 的size
       int size = 0;
        ListNode cur = head;
        while (cur != null ) {
            cur = cur.next;
            size++;
        }
        ListNode temp = head;
        int index = size - n;
        for (int i = 1; i < index; i++) {
            temp = temp.next;
        }
        temp.next = temp.next.next;

        return head;

    }

    public static void main(String[] args) {
//        ListNode head = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5, null)))));
        ListNode head = new ListNode(1, null);
        ListNode res = removeNthFromEnd(head, 1);
        while (res != null) {
            System.out.println(res.val);
            res = res.next;
        }
        }

    public static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
}
